/************************************************************************
 * The code is owned by mainframer.cn and Shang Tao
 * Without aproval of mainframer.cn and Shang Tao, remove the copyright
 * infomation is regarded invassion of proprietary copyright
 ***********************************************************************/
package com.mr.qa.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import com.mr.qa.GlobalConfigs;

/**
 * check the number of the questions of a user on one day
 * the data is stored in memory rather than persisted
 */
public class DailyAddQuesionCheck
{
    static public HashMap userQuestionSize;
    static public HashMap userImageSize;
    static public String recordDateString;//yyyy-mm-dd
    
    private static void initImage(int userId){
    	Date calendar = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(calendar);
        boolean ifNewRecord = false;
        if(!dateString.equals(recordDateString) || recordDateString == null)
        {
            ifNewRecord = true;
        }
        if(ifNewRecord || userImageSize == null)
        {
        	userImageSize = new HashMap();
            recordDateString = dateString;
            userImageSize.put(userId,0);
        }
    }
    
    private static boolean updateImage(int userId){
    	Date calendar = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(calendar);
        boolean ifNewRecord = false;
        if(!dateString.equals(recordDateString) || recordDateString == null)
        {
            ifNewRecord = true;
        }
        if(ifNewRecord || userImageSize == null)
        {
        	userImageSize = new HashMap();
            recordDateString = dateString;
            userImageSize.put(userId,1);
        }
        else
        {
            Integer number = (Integer)userImageSize.get(userId);
            if(number == null)
            	userImageSize.put(userId,1);
            else
            {
                if(number >= GlobalConfigs.NUMBER_IMAGE_A_USER_ONE_DAY)
                    return false;
                else
                {
                	userImageSize.put(userId,number+1);
                }
            }
        }
        return true;
    }
    
    private static boolean checkImage(int userId)
    {
            if(userImageSize == null || userImageSize.size() == 0)
                return true;
            Integer number = (Integer)userImageSize.get(userId);
            if(number != null && 
            number >= GlobalConfigs.NUMBER_IMAGE_A_USER_ONE_DAY)
                    return false;
            
            return true;
    }
    
    private static void init(int userId){
    	Date calendar = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(calendar);
        boolean ifNewRecord = false;
        if(!dateString.equals(recordDateString) || recordDateString == null)
        {
            ifNewRecord = true;
        }
        if(ifNewRecord || userImageSize == null)
        {
        	userQuestionSize = new HashMap();
            recordDateString = dateString;
            userQuestionSize.put(userId,0);
        }
    }
    
    //add the number
    private static boolean update(int userId)
    {
        Date calendar = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(calendar);
        boolean ifNewRecord = false;
        if(!dateString.equals(recordDateString) || recordDateString == null)
        {
            ifNewRecord = true;
        }
        if(ifNewRecord || userQuestionSize == null)
        {
            userQuestionSize = new HashMap();
            recordDateString = dateString;
            userQuestionSize.put(userId,1);
        }
        else
        {
            Integer number = (Integer)userQuestionSize.get(userId);
            if(number == null){
                userQuestionSize.put(userId,1);
            }
            else
            {
                if(number >= GlobalConfigs.NUMBER_QUESTIONS_A_USER_ONE_DAY)
                    return false;
                else
                {
                    userQuestionSize.put(userId,number+1);
                }
            }
        }
        return true;
    }
    //check whether the user can post
    private static boolean check(int userId)
    {
            if(userQuestionSize == null || userQuestionSize.size() == 0)
                return true;
            Integer number = (Integer)userQuestionSize.get(userId);
            if(number != null && 
            number >= GlobalConfigs.NUMBER_QUESTIONS_A_USER_ONE_DAY)
                    return false;
            
            return true;
    }

}
